<?php
$xh=$_COOKIE['xh'];
$pwd=$_COOKIE['pwd'];
$rememberMe=$_COOKIE['rememberMe'];
if($_POST){
    try {
        echo '<pre>', print_r($_POST, true), '</pre>';
        if ($_POST['rememberMe']) {//记住登录信息
            setcookie('xh', $_POST['xh'], time() + 7 * 24 * 3600);
            setcookie('pwd', $_POST['pwd'], time() + 7 * 24 * 3600);
            setcookie('rememberMe', $_POST['rememberMe'], time() + 7 * 24 * 3600);

        } else {//不用记住登录信息，删除cookie信息
            setcookie('xh');
            setcookie('pwd');
            setcookie('rememberMe');
        }
        $db = new PDO("mysql:host=localhost; dbname=db2;", 'root', '12qwas');
        $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
        $ps = $db->prepare('select * from students where xh=? and pwd=?');
        $ps->execute([$_POST['xh'], $_POST['pwd']]);
        $r = $ps->fetch();

        if ($r === false) throw  new Exception('你输入的用户名或口令不正确');
        header('Location: index.php');
        return;
    }catch (Throwable $e){
        $msg=$e->getMessage();

    }
}
?>


